﻿$(function () {

    $.ajaxSetup({ cache: false });

    $("a[data-modal]").on("click", function (e) {
       
        //$("#wait4").attr("style", "display:block;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
        $('#myModalContent').load(this.href, function () {

           
            $('#myModal').modal({
                /*backdrop: 'static',*/
                keyboard: true
            }, 'show');

            bindForm(this);
            //$("#wait4").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
        });
        
        return false;
    });


    $("a[delete-modal]").on("click", function (e) {
        //$("#wait4").attr("style", "display:block;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");

        $('#deleteModalContent').load(this.href, function () {

            //  $("#wait1").attr("style", "display:block;position:absolute;top:" + 200 + ";left:" + 200 + ";padding:2px; z-index:99999");

            $('#deleteModal').modal({
                /*backdrop: 'static',*/
                keyboard: true
            }, 'show');

            bindFormDelete(this);
            //$("#wait4").attr("style", "display:none;position:absolute;top:25%;left:50%;padding:2px; z-index:99999");
        });
        // $("#wait1").attr("style", "display:none;position:absolute;top:" + 200 + ";left:" + 200 + ";padding:2px; z-index:99999");
        return false;
    });



});

function bindForm(dialog) {

    $('form', dialog).submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.success) {
                    $('#myModal').modal('hide');
                    //Refresh
                    location.reload();
                } else {
                    $('#myModalContent').html(result);
                    bindForm();
                }
            }
        });

   

        return false;
    });
}


function bindFormDelete(dialog) {

    $('form', dialog).submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.success) {
                    $('#deleteModal').modal('hide');
                    //Refresh
                    location.reload();
                } else {
                    $('#deleteModalContent').html(result);
                    bindForm();
                }
            }
        });

    

        return false;
    });
}